function [R] = bridge_adjust(command)
% adjust the bridge value for a data set
% returns the final R value selected if OK,
% returns a NULL value if cancel.
% displays the first 20 msec of the data set for visualization
% 12/16/1998 P. Manis
global BRIDGE

if(isempty(BRIDGE))
    BRIDGE.factor = 1;
end;
R=[];
switch(command)
    case 'init'
        make_bridge; % create the bridge_control window
        re_display;

    case 'OK'
        htag=findobj('Tag', 'br_Slider');
        R = get(htag, 'Value'); % get the current value for R
        edit_control('NewBridge', R); % call them back to update the bridge value, and pass the value
        close;

    case 'display'
        re_display; % display the data (again...)

    case 'dismiss'
        R=[]; % set to null value. Signals do not change...
        close;
        clear d;
        cmd = 0; % force a return

    case 'show'
        update_slider;

    case 'auto'
        R=autobridge;
        htag=findobj('Tag', 'br_Slider');
        set(htag, 'Value', R); % get the current value for R
        update_slider;
        re_display;
    case 'show_more'
        BRIDGE.factor = BRIDGE.factor*2;
        re_display;
    case'show_less'
        BRIDGE.factor = BRIDGE.factor/2;
        re_display;
    case 'show_standard'
        BRIDGE.factor = 1;
        re_display;

    otherwise
end


%----------------------------------------------------------------
% make the bridge balance window and controls
%----------------------------------------------------------------

function make_bridge()

% read it from the disk instead - after "guiding" it.
%
    h = findobj('Tag', 'Bridge'); % check for pre-existing window
    if(isempty(h)) % if none, make one
        if(isempty(h))
            open('bridge.fig');
        end;
        h = findobj('tag', 'Bridge');
        % Use system color scheme for figure:
    end
    figure(h); % otherwise, select it
    set(h,'Color',get(0,'defaultUicontrolBackgroundColor'));

    return;
    






function update_slider()
h = findobj('Tag', 'br_Slider_info');
if(~ishandle(h))
    return;
end
h1=findobj('Tag', 'br_Slider');
if(~ishandle(h1))
    return;
end
v=get(h1, 'Value');
set(h, 'String', sprintf('%6.1f', v));




%------------------------------------------------
% re display the voltage data in the window - just the first 20 msec...
%
function re_display()

%h=findobj('Tag', 'CTL');
%if(~ishandle(h))
%    return;
%end
%sel = get(h, 'Value'); % read control entry that is selected...
h1 = findobj('Tag', 'br_Slider');
bridge = get(h1, 'Value');

datac('bridge_adjust', bridge); % call the routine that has access to the data

return;

function oldmakebridg()
scrsz = get(0,'ScreenSize'); % determine the position of the figure in the system window
size=0.66;
scrb = scrsz(4)*((1-size)/2);
scrl = scrsz(3)*((1-size)/2);
scrwd = scrsz(3)*size;
scrht = scrsz(4)*size;



b_ht = 1.7;
pad_check = 7; pad_edit = 5; pad_list = 5; pad_pop = 7;
pad_push = 4; pad_radio = 7; pad_static = 4;
but_vpad = 0.125;
but_hpad = 1;

but_vspc = b_ht + but_vpad; % space to allocate for each button or field

% build the button list (only 1 column of buttons in this version)
buttonlist = [];
buttonlist = add_button(buttonlist, 'OK', 'bridge_adjust OK', 'Accept Current Value', 'OK', 'on');
buttonlist = add_button(buttonlist, 'Display', 'bridge_adjust display', 'Display data with current value', 'Display', 'on');
buttonlist = add_button(buttonlist, 'Auto', 'bridge_adjust auto', 'Try automatic algorithm', 'Auto', 'on');
buttonlist = add_button(buttonlist, 'Dismiss', 'bridge_adjust dismiss', 'Close and do not update', 'Dismiss', 'on');

maxtitle = 0;
for i=1:length(buttonlist)
    l = length(buttonlist(i).title);
    if(l > maxtitle) maxtitle = l;
    end
end
but_width = maxtitle+2*but_hpad;

h0 = figure('Color', 'white', ...
    'Colormap',[1,1,1], ...
    'Position', [scrl, scrb, scrwd, scrht], ...
    'Name', 'Bridge', ...
    'NumberTitle', 'off', ...
    'Tag','Bridge');

set(h0, 'Units', 'characters');
figsz=get(h0, 'Position');

button_x = figsz(3) - but_width-but_hpad;  % place the buttons to the right side of the display
button_y = figsz(4)*0.75-1;

% now put things in the figure
% first the buttons

%--------------
% command buttons are here

cmd_vspc = but_vspc + 2*but_vpad; % extra padding
for i=1:length(buttonlist)
    b_y = button_y-i*cmd_vspc;
    if(~isempty(buttonlist(i).pos))
        b_y = 1;
    end
    h1=uicontrol('Parent', h0, ...
        'Units', 'characters', ...
        'FontUnits', 'points', ...
        'FontName', 'Arial', ...
        'FontSize', 9, ...
        'ForegroundColor', 'blue', ...
        'Position', [button_x b_y but_width+0.5 b_ht], ...
        'String', buttonlist(i).title, ...
        'ToolTipString', buttonlist(i).tooltip, ...
        'Callback', buttonlist(i).callback, ...
        'Enable', buttonlist(i).enable, ...
        'Tag', buttonlist(i).tag);
end

% place a slider in the window just below the last button

slider_y = button_y-length(buttonlist)*cmd_vspc;
slider_x = figsz(3) - 2*(but_width-but_hpad); % make it a reasonable size

h2=uicontrol('Parent', h0, ...
    'Units', 'characters', ...
    'FontUnits', 'points', ...
    'FontName', 'Arial', ...'
    'FontSize', 9, ...
    'Position', [slider_x, slider_y, b_ht, but_width*3], ...
    'Style', 'Slider', ...
    'Min', -50, 'Max', 50, ...
    'SliderStep', [0.001 0.05], ...
    'Value', 0, ...
    'String', 'Im a slider', ...
    'Callback', 'bridge_adjust show', ...
    'Tag', 'Slider');

h2=uicontrol('Parent', h0, ...
    'Backgroundcolor', 'white', ...
    'Units', 'characters', ...
    'FontUnits', 'points', ...
    'FontName', 'Arial', ...'
    'FontSize', 9, ...
    'Position', [slider_x-but_width/2, slider_y+but_width*1.5+b_ht, but_width, b_ht], ...
    'Style', 'text', ...
    'String', '0', ...
    'Tag', 'Slider_info');

